;;; set-cc-dev.el - settings for c development.
;;;  
;;; Copyright (C) 2011 Jeffy Du
;;;  
;;; Author: Jeffy Du (cmdxiaoha@163.com)
;;; Create: 2011-12-23 23:38:41
;;; Last Modified: 2011-12-23 23:40:26
;;;  
;;; History:
;;; --------
;;; 2011-12-23  v0.1  Jeffy Du (cmdxiaoha@163.com)
;;;     1. Initial revision.

(require 'cc-mode)
(require 'auto-complete-clang)

;; My cc-mode settings.
(defun my-c-mode-common-hook ()
  "Settings for cc mode."
  
  ;; use gtags to browse code.
  (gtags-mode t)

  ;; use clang to complete code.
  (setq ac-sources (append '(ac-source-clang) ac-sources))
  ;; read clang flags file, if file not exists, then goto the parent directory to find file.
  (defun set-clang-flags (file)
    (let ((orignal-dir default-directory)
          (found t))
      (while (and (not (file-readable-p file)) found)
        (if (string= default-directory "/")
            (setq found nil)
          (cd "..")))
      (if found
          (save-excursion
            (let ((buffer (find-file-noselect file)))
              (set-buffer buffer)
              (setq ac-clang-flags (split-string (buffer-string)))
              (kill-buffer buffer))))
      (cd orignal-dir)))
  (set-clang-flags ".clang_complete")

  ;; C coding style.
  (c-set-style "K&R")
  (c-toggle-hungry-state)
  (setq c-basic-offset 4)

  ;; compile & debug.
  (setq compile-command "make")
  (define-key c-mode-base-map [(f7)] 'compile)
  (define-key c-mode-base-map [(f8)] 'gdb)

  ;; key bindings for c code.
  (define-key c-mode-base-map (kbd "C-c C-c") 'my-comment-or-uncomment-region-or-line)
)

(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
